﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using DAL = IrisGenerator.Common.DatabaseModel.DataAccess;
using IrisGenerator.Common.Tools;
using System.Data;

namespace IrisGenerator.Common.DatabaseModel
{
    public class Constraint
    {
        private Collection<ConstraintKey> _keys = null;

        public Guid? Identifier { get; set; }
        public string Name { get; set; }
        public Collection<ConstraintKey> Keys
        {
            get
            {
                if (_keys == null)
                    _keys = ConstraintKey.GetConstraintKeys(this);
                
                return _keys;
            }
        }
        public ConstraintType ConstraintType { get; set; }
        public Table Table { get; set; }

        public virtual Guid? Insert()
        {
            DAL.Constraint dbo = null;
            try
            {
                dbo = new DAL.Constraint();
                this.Identifier=dbo.Constraint_Insert(this.Name,this.Table.TabId,this.ConstraintType.Identifier);
                return this.Identifier;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
            return null;
        }

        public static Constraint Load(Guid? id)
        {
            DAL.Constraint dbo = null;
            Constraint returnValue;
            try
            {
                dbo = new DAL.Constraint();
                DataSet ds = dbo.SelectOne(id);
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    returnValue = new Constraint();
                    returnValue.Fill(ds.Tables[0].Rows[0]);
                    return returnValue;

                }
                return null;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
            return null;
        }
        
        private void Clean()
        {
            this.Identifier = null;
            this.Name = string.Empty;
            this._keys = null;
            this.ConstraintType = null;
            this.Table = null;
        }

        public void Fill(System.Data.DataRow dr)
        {
            this.Clean();
            if ((dr["CONS_ID"] != System.DBNull.Value))
            {
                this.Identifier = ((System.Nullable<System.Guid>)(dr["CONS_ID"]));
            }
            if ((dr["CONS_TYP_ID"] != System.DBNull.Value))
            {
                this.ConstraintType = ConstraintType.Load((System.Nullable<System.Guid>)(dr["CONS_TYP_ID"]));
            }
            if ((dr["TAB_ID"] != System.DBNull.Value))
            {
                this.Table = Table.Load((System.Nullable<System.Guid>)(dr["TAB_ID"]));
            }
            if ((dr["CONS_NAME"] != System.DBNull.Value))
            {
                this.Name = ((string)(dr["CONS_NAME"]));
            }
            
        }

        
    }
}
